﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Ds17.Ftp;

public partial class ftp : System.Web.UI.Page
{
    private const string FTPController = "FtpController";
    

    private void GetFtpFilesAndFolders(FtpController ftp)
    {
        string[] directories = ftp.GetFtpDirectories();
        List<string> floderList = new List<string>();
        List<Files> files = new List<Files>();
        if (directories != null)
        {
            floderList = ftp.GetFolders(directories);
            floderList.Insert(0, "Go up a level");
            gvFolder.DataSource = floderList;
            gvFolder.DataBind();

            files = ftp.GetFiles(directories);
            gvFile.DataSource = files;
            gvFile.DataBind();
        }
        else
        {
            floderList.Insert(0, "Go up a level");
            gvFolder.DataSource = floderList;
            gvFolder.DataBind();

            gvFile.DataSource = files;
            gvFile.DataBind();
        }
    }

    private void DeleteLocalFile(string filePath)
    {
        System.IO.File.Delete(filePath);
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnLogIn_Click(object sender, EventArgs e)
    {
        string serverName = txtServerName.Text.Trim();
        string userName = txtUserName.Text.Trim();
        string password = txtPassWord.Text.Trim();
        FtpController ftp = new FtpController();
        ftp.ServerName = serverName;
        ftp.UserName = userName;
        ftp.Password = password;
        if (ftp.IsConnected())
        {
            string str = "connected";
            mvFtp.ActiveViewIndex = 1;
            ftp.CurrentFtpPath = "";
            string[] directories = ftp.GetFtpDirectories();

            List<string> floderList = ftp.GetFolders(directories); //directories.ToList();
            floderList.Insert(0, "Go up a level");
            gvFolder.DataSource = floderList;
            gvFolder.DataBind();

            List<Files> files = ftp.GetFiles(directories);
            gvFile.DataSource = files;
            gvFile.DataBind();

            lblDirectory.Text = "/";
            Session[FTPController] = ftp;
        }
    }

    protected void gvFolder_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
        {
            string data = (string)e.Row.DataItem;
            LinkButton lnkFolderName = (LinkButton)e.Row.FindControl("lnkFolderName");
            Image imgFolder = (Image)e.Row.FindControl("imgFolder");
            ImageButton imgDelete = (ImageButton)e.Row.FindControl("imgDelete");

            lnkFolderName.Text = data;
            lnkFolderName.Font.Underline = true;
            lnkFolderName.CommandArgument = data;
            imgDelete.CommandArgument = data;

            if (data == "Go up a level")
            {
                imgFolder.ImageUrl = "~/images/Up.gif";
            }
            else
            {
                imgFolder.ImageUrl = "~/images/folder.gif";
            }
        }
    }

    protected void gvFolder_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            string data = e.CommandArgument.ToString();
            if (data == "Go up a level")
            {
                //Goto upper level
                //data = data.Substring(data.LastIndexOf('/'));
                FtpController ftp = (FtpController)Session[FTPController];
                if (ftp.CurrentFtpPath.Length > 1)
                {
                    int len = ftp.CurrentFtpPath.LastIndexOf('/');
                    string NewPath = ftp.CurrentFtpPath.Substring(0, len);
                    ftp.CurrentFtpPath = NewPath;
                    GetFtpFilesAndFolders(ftp);
                    lblDirectory.Text = string.IsNullOrEmpty(ftp.CurrentFtpPath) ? "/" : ftp.CurrentFtpPath;
                    Session[FTPController] = ftp;
                }
            }
            else
            {
                //Go into selected folder
                FtpController ftp = (FtpController)Session[FTPController];
                ftp.CurrentFtpPath = ftp.CurrentFtpPath + "/" + data;
                GetFtpFilesAndFolders(ftp);
                lblDirectory.Text = ftp.CurrentFtpPath;
                Session[FTPController] = ftp;

            }
        }
        else if (e.CommandName == "Delete")
        {
            string data = e.CommandArgument.ToString();
            if (!string.IsNullOrEmpty(data))
            {
                FtpController ftp = (FtpController)Session[FTPController];
                bool delete = ftp.DeleteDirectoryFromFTP(data);
                if (delete)
                {
                    GetFtpFilesAndFolders(ftp);
                }
            }
        }
    }

    protected void gvFolder_RowEditing(object sender, GridViewEditEventArgs e)
    {
        // Keep this event
    }
    protected void gvFolder_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // Keep this event
    }

    protected void gvFile_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            string data = e.CommandArgument.ToString();
            if (!string.IsNullOrEmpty(data))
            {
                FtpController ftp = (FtpController)Session[FTPController];
                ftp.FileName = data;
                byte[] strFile = ftp.DownloadFileFromFTP();
                if (strFile != null)
                {
                    Response.AppendHeader("content-disposition", "attachment; filename=" + data);
                    Response.ContentType = "application/octet-stream";
                    Response.BinaryWrite(strFile);
                    Response.End();
                }
            }
        }
        else if (e.CommandName == "Delete")
        {
            string data = e.CommandArgument.ToString();
            if (!string.IsNullOrEmpty(data))
            {
                FtpController ftp = (FtpController)Session[FTPController];
                bool delete = ftp.DeleteFileFromFTP(data);
                if (delete)
                {
                    GetFtpFilesAndFolders(ftp);
                }
            }
        }
    }
    protected void gvFile_RowEditing(object sender, GridViewEditEventArgs e)
    {
        // Keep this event
    }

    protected void gvFile_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // Keep this event
    }

    protected void gvFile_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
        {
            Files data = (Files)e.Row.DataItem;
            LinkButton lnkFileName = (LinkButton)e.Row.FindControl("lnkFileName");
            Image imgFile = (Image)e.Row.FindControl("imgFile");
            ImageButton imgFileDelete = (ImageButton)e.Row.FindControl("imgFileDelete");
            lnkFileName.Text = data.FileName;
            lnkFileName.Font.Underline = true;
            lnkFileName.CommandArgument = data.FileName;
            imgFileDelete.CommandArgument = data.FileName;
            imgFile.ImageUrl = "~/images/file.gif";
        }
    }

    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fuFtp.HasFile)
        {
            FtpController ftp = (FtpController)Session[FTPController];
            string lPath = Server.MapPath("Uploads");
            string fileName = fuFtp.PostedFile.FileName;
            fuFtp.SaveAs(lPath + "\\" + fileName);
            fuFtp.Dispose();

            ftp.FileName = fileName;
            ftp.UploadFileByFTP(lPath + "\\" + fileName);
            string[] directories = ftp.GetFtpDirectories();
            List<Files> files = ftp.GetFiles(directories);
            gvFile.DataSource = files;
            gvFile.DataBind();
            try
            {
                DeleteLocalFile(lPath + "\\" + fileName);
            }
            catch
            {
            }
        }
    }

    protected void btnCreateDirectory_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtDirectory.Text))
        {
            string NewDirectory = txtDirectory.Text.Trim();
            FtpController ftp = (FtpController)Session[FTPController];
            ftp.CreateFtpDirectories(ftp.CurrentFtpPath + "/" + NewDirectory);
            string[] directories = ftp.GetFtpDirectories();
            if (directories != null)
            {
                List<string> floderList = ftp.GetFolders(directories);
                floderList.Insert(0, "Go up a level");
                gvFolder.DataSource = floderList;
                gvFolder.DataBind();
            }
        }
    }
}